package gov.pr.celepar.sentinela.admin.form.restricao;

import gov.pr.celepar.sentinela.admin.form.BaseForm;

import java.io.Serializable;
import java.util.StringTokenizer;

public class RestricaoIPSistemaForm extends BaseForm implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5729173861769156378L;

	private String codSistema;

	private String nomeSistema;

	private String enderecoIP;

	private String codRestricao;

	private String ip1;

	private String ip2;

	private String ip3;

	private String ip4;

	private String ip5;

	private boolean ativa;

	public String getCodSistema() {
		return this.codSistema;
	}

	public void setCodSistema(String cod) {
		this.codSistema = cod;
	}

	public String getIp1() {
		return ip1;
	}

	public void setIp1(String ip1) {
		this.ip1 = ip1;
	}

	public String getIp2() {
		return ip2;
	}

	public void setIp2(String ip2) {
		this.ip2 = ip2;
	}

	public String getIp3() {
		return ip3;
	}

	public void setIp3(String ip3) {
		this.ip3 = ip3;
	}

	public String getIp4() {
		return ip4;
	}

	public void setIp4(String ip4) {
		this.ip4 = ip4;
	}

	public String getIp5() {
		return ip5;
	}

	public void setIp5(String ip5) {
		this.ip5 = ip5;
	}

	public boolean isAtiva() {
		return ativa;
	}

	public void setAtiva(boolean ativa) {
		this.ativa = ativa;
	}

	public String getCodRestricao() {
		return codRestricao;
	}

	public void setCodRestricao(String codRestricao) {
		this.codRestricao = codRestricao;
	}

	public String getEnderecoIP() {
		return enderecoIP;
	}

	public void setEnderecoIP(String enderecoIP) {
		this.enderecoIP = enderecoIP;
		if (enderecoIP != null) {
			StringTokenizer tokenizer = new StringTokenizer(enderecoIP, ".");
			String[] partes = new String[tokenizer.countTokens()];
			int i = 0;
			while (tokenizer.hasMoreTokens()) {
				partes[i] = tokenizer.nextToken();
				i++;
			}
			if (partes.length > 0)
				setIp1(partes[0]);
			if (partes.length > 1)
				setIp2(partes[1]);
			if (partes.length > 2)
				setIp3(partes[2]);
			if (partes.length > 3) {
				String parte = partes[3];
				setIp4(parte.substring(0, parte.indexOf("/")));
				setIp5(parte.substring(parte.indexOf("/") + 1));
			}
		}
	}

	public String getNomeSistema() {
		return nomeSistema;
	}

	public void setNomeSistema(String nomeSistema) {
		this.nomeSistema = nomeSistema;
	}

	public String getIPFormatado() {
		StringBuffer ip = new StringBuffer();

		ip.append(this.getIp1());
		if (this.getIp2() != null && !this.getIp2().trim().equals("")) {
			ip.append(".").append(this.getIp2());
			if (this.getIp3() != null && !this.getIp3().trim().equals("")) {
				ip.append(".").append(this.getIp3());
				if (this.getIp4() != null && !this.getIp4().trim().equals("")) {
					ip.append(".").append(this.getIp4());
				}
			}
		}
		if (this.getIp5() != null && !this.getIp5().trim().equals("")) {
			ip.append("/").append(this.getIp5());
		}

		return ip.toString();
	}
}